package
{
	import com.greensock.TweenLite;
	import com.sociodox.theminer.TheMiner;
	
	import flash.display.Sprite;
	
	import assets.AssetsCore;
	import assets.AssetsUI;
	
	import org.agony2d.Agony;
	import org.agony2d.StatsKai;
	import org.agony2d.control.KeyboardManager;
	import org.agony2d.loader.LoaderManager;
	import org.agony2d.notify.AEvent;
	import org.agony2d.notify.ATouchEvent;
	import org.agony2d.utils.getClassName;
	import org.agony2d.view.AgonyUI;
	import org.agony2d.view.ButtonType;
	import org.agony2d.view.core.IModule;
	
	import states.*;

	[SWF(width = '960' ,height = '600', frameRate = '60')]
public class DesktopUITest extends Sprite 
{
	
	public function DesktopUITest() 
	{
		this.init()
	}
	
	public var mStateList:Array =
	[
		//ScrollPaneUIState,
		ImageUIState,
		//StartingState,
		AnimeState,
		NineScaleUIState,
		//TextUIState,
		RangeUIState
		//MultiUIState
	]
	
	
	private function init() : void
	{
		// Engine !!
		Agony.startup(this.stage)
		
		// UI...
		AgonyUI.startup(false, 960, 600)
		
		// keyboard...
		KeyboardManager.getInstance().initialize()
		
		// Stats
		//this.addChild(new StatsKai)
		this.addChild(new TheMiner)
		
		// Assets...
		var assetsList:Array
		var i:int, l:int
		
		
		
		assetsList = AssetsCore.getAssetList().concat(AssetsUI.getAssetList())
		l = assetsList.length
		while (--l > -1)
		{
			LoaderManager.getInstance().getBytesLoader(assetsList[l])
		}
		
		LoaderManager.getInstance().addCompleteListener(onAssetsComplete)
	}
	
	private function onAssetsComplete() : void
	{
		LoaderManager.getInstance().reomveCompleteListener(onAssetsComplete)
		
		AgonyUI.addImageButtonData(AssetsUI.AT_btn_yellow, 'AT_btn_yellow_1', ButtonType.BUTTON_HAS_INVALID | ButtonType.LEAVE_LEAVE)
		AgonyUI.addImageButtonData(AssetsUI.AT_btn_yellow, 'AT_btn_yellow_2', ButtonType.BUTTON_HAS_INVALID | ButtonType.LEAVE_PRESS)
		AgonyUI.addImageButtonData(AssetsUI.AT_btn_yellow, 'AT_btn_yellow_3', ButtonType.BUTTON_HAS_INVALID | ButtonType.PRESS_PRESS)
		AgonyUI.addImageButtonData(AssetsUI.AT_checkbox, 'AT_checkbox_1', ButtonType.CHECKBOX_HAS_INVALID | ButtonType.LEAVE_LEAVE)
		AgonyUI.addImageButtonData(AssetsUI.AT_checkbox, 'AT_checkbox_2', ButtonType.CHECKBOX_HAS_INVALID | ButtonType.LEAVE_PRESS)
		AgonyUI.addImageButtonData(AssetsUI.AT_checkbox, 'AT_checkbox_3', ButtonType.CHECKBOX_HAS_INVALID | ButtonType.PRESS_PRESS)
		
		AgonyUI.addMovieClipButtonData('Btn_A', 'Btn_A', ButtonType.BUTTON_HAS_INVALID | ButtonType.LEAVE_LEAVE)
		AgonyUI.addMovieClipButtonData('Select_A', 'Select_A', ButtonType.CHECKBOX_HAS_INVALID | ButtonType.LEAVE_LEAVE)
		
		//AgonyUI.addNullPressedListener(
		
		// Section...
		var sections:Array = AssetsCore.getSectionXML()
		var i:int, l:int = sections.length
		var stateType:Class
		var name:String
		
		while (i < l)
		{
			Agony.registerSections(XML(sections[i++]))
		}
		
		mLength = mStateList.length
		l = mStateList.length
		
		while(--l>-1)
		{
			stateType = mStateList[l];
			name = getClassName(stateType)
			AgonyUI.addModule(name, stateType)
			AgonyUI.getModule(name).addEventListener(AEvent.ENTER_STAGE, function(e:AEvent):void
			{
				trace(AgonyUI.cacheToString())
			})
		}
		
		// Keyboard !!
		KeyboardManager.getInstance().getState().press.addEventListener('K', function(e:AEvent):void
		{
			AgonyUI.killAllModules()
		})
		KeyboardManager.getInstance().getState().press.addEventListener('T', function(e:AEvent):void
		{
			TweenLite.paused = !TweenLite.paused
		})
		KeyboardManager.getInstance().getState().press.addEventListener('UP', function(e:AEvent):void
		{
			Agony.process.timeScaleFactor -= 0.1
			trace(Agony.process.timeScaleFactor)
		})
		KeyboardManager.getInstance().getState().press.addEventListener('DOWN', function(e:AEvent):void
		{
			Agony.process.timeScaleFactor += 0.1
			trace(Agony.process.timeScaleFactor)
		})
		KeyboardManager.getInstance().getState().press.addEventListener('LEFT', function(e:AEvent):void
		{
			if (--mIndex < 0)
			{
				mIndex=mLength - 1
			}
			changeState()
		})
		KeyboardManager.getInstance().getState().press.addEventListener('RIGHT', function(e:AEvent):void
		{
			if (++mIndex >= mLength)
			{
				mIndex=0
			}
			changeState()
		})
		changeState()
	}
	
	private function changeState():void
	{
		var module:IModule
		var type:Class
		
		module = AgonyUI.getModule(mModuleName)
		if (module)
		{
			module.close()
		}
		type = mStateList[mIndex] as Class
		
		mModuleName = getClassName(type)
		AgonyUI.getModule(mModuleName).popup(-1,true,[44])
		trace('[module] - ' + mModuleName)
	}

	private var mIndex:int
	private var mLength:int
	private var mModuleName:String	
}
}